package com.ate.ui;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.view.View;

import com.ate.ui.fragments.ClassifyFragment;
import com.ate.ui.fragments.MainFragment;
import com.ate.ui.fragments.MidFragment;
import com.ate.ui.fragments.MoreFragment;
import com.ate.ui.fragments.UserFragment;
import com.ate.javamvp.R;
import com.ate.bean.TabBean;
import com.ate.lib_core.http.mvp.view.BaseActity;
import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class MainActivity extends BaseActity {

    private ArrayList<CustomTabEntity> tabEntitys;
    private MainFragment mainFragment;
    private com.flyco.tablayout.CommonTabLayout common;
    private ClassifyFragment classifyFragment;
    private MidFragment midFragment;
    private MoreFragment moreFragment;
    private UserFragment userFragment;
    private FloatingActionButton buycar;


    @Override
    public int bandlayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        buycar = (FloatingActionButton) findViewById(R.id.buycar);
        common = (CommonTabLayout) findViewById(R.id.common);
        addfragment();
        addCommon();
        buycar.setBackgroundColor(Color.WHITE);
        buycar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,BuyCarActivity.class));
            }
        });
    }

    private void addfragment() {
        mainFragment = new MainFragment();
        classifyFragment = new ClassifyFragment();
        midFragment = new MidFragment();
        moreFragment = new MoreFragment();
        userFragment = new UserFragment();

        getSupportFragmentManager().beginTransaction().add(R.id.act_main_layout, mainFragment)
                .add(R.id.act_main_layout, classifyFragment)
                .add(R.id.act_main_layout, midFragment)
                .add(R.id.act_main_layout, moreFragment)
                .add(R.id.act_main_layout, userFragment)
                .hide(classifyFragment).hide(midFragment).hide(moreFragment).hide(userFragment)
                .commit();

    }

    private void addCommon() {
        tabEntitys = new ArrayList<>();
        tabEntitys.add(new TabBean("首页",R.mipmap.mian2,R.mipmap.main));
        tabEntitys.add(new TabBean("分类",R.mipmap.class2,R.mipmap.class1));
        tabEntitys.add(new TabBean("",R.mipmap.mid2,R.mipmap.mid));
        tabEntitys.add(new TabBean("市集",R.mipmap.more2,R.mipmap.more));
        tabEntitys.add(new TabBean("我的",R.mipmap.user2,R.mipmap.user));
        common.setTabData(tabEntitys);
        common.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position){
                    case 0:
                        replaceFragment(mainFragment);
                        break;
                    case 1:
                        replaceFragment(classifyFragment);
                        break;
                    case 2:
                        replaceFragment(midFragment);
                        break;
                    case 3:
                        replaceFragment(moreFragment);
                        break;
                    case 4:
                        if (jsonobject==null){
                            startActivity(new Intent(MainActivity.this,LoginActivity.class));
                        } else {
                            replaceFragment(userFragment);
                        }
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


    }

    private void replaceFragment(Fragment fragment){
       getSupportFragmentManager().beginTransaction()
               .hide(mainFragment).hide(classifyFragment).hide(moreFragment)
               .hide(midFragment).hide(userFragment).show(fragment).commit();
    }
    String jsonobject ="";
    @Override
    public void initData() {


    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}